/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.config;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Darlan
 */
public class RelatoriosUtilities
{

    private static RelatoriosUtilities instance;

    /**
     * 
     */
    public static RelatoriosUtilities getInstance()
    {
        if ( instance == null )
        {
            instance = new RelatoriosUtilities();
        }

        return instance;
    }

    private RelatoriosUtilities() {}
    
    /**
     * 
     * @param parameters
     * @throws Exception 
     */
    public void print( InputStream report )
    {
        print( report, null );
    }
    
    /**
     * 
     * @param report
     * @param parameters
     * @throws Exception 
     */
    public void print( InputStream report, Map parameters )
    {
        try
        {
            if ( parameters == null )
            {
                parameters = new HashMap();
            }

            JasperReport jasperReport = JasperCompileManager.compileReport( report );

            JasperPrint print = JasperFillManager.fillReport( 
                    jasperReport, parameters, Database.getInstance().getConnection() );

            JasperViewer viewer = new JasperViewer( print, false );
            viewer.setVisible( true );
        }
        
        catch ( Exception e )
        {
            Utilities.handleException( e );
        }
    }
}
